﻿using EagleProject.LinQDataContext;
using EagleProject.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EagleProject.Controllers
{
    public class AdminController : MyController
    {
        private string GetUploadDirectory()
        {
            return "~/Upload/Accident/";
        }

        public ActionResult Success()
        {
            if (!IsAuthenticated()) return RedirectToLogin(@"~/Admin/Success");
            if (!IsAdmin()) return RedirectToAccessDenied();

            return View();
        }

        public ActionResult Index()
        {
            if (!IsAuthenticated()) return RedirectToLogin(@"~/Admin/Index");
            if (!IsAdmin()) return RedirectToAccessDenied();

            return View();
        }

        //
        // GET: /Admin/CreateAccidentRecord
        public ActionResult CreateAccidentRecord()
        {
            if (!IsAuthenticated()) return RedirectToLogin(@"~/Admin/CreateAccidentRecord");
            if (!IsAdmin()) return RedirectToAccessDenied();

            ViewBag.ListState = new SelectList(DatabaseController.GetStates(), "ID", "Name");
            ViewBag.ListLGAs = new SelectList(DatabaseController.GetLGAs(), "ID", "Name");

            return View();
        }

        //
        // POST: /Admin/CreateAccidentRecord
        [HttpPost]
        public ActionResult CreateAccidentRecord(AccidentRecord model)
        {
            if (!IsAuthenticated()) return RedirectToLogin(@"~/Admin/CreateAccidentRecord");
            if (!IsAdmin()) return RedirectToAccessDenied();

            string urls = HandleFileUploadsToJson(GetUploadDirectory());
            model.AccidentPictureURL = urls;
            model.AdminID = GetUserInformation().ID;
            DatabaseController.CreateAccidentRecord(model);

            return RedirectToAction("Success");
        }

        //
        // GET: /Admin/EditAccidentRecord/{id}
        public ActionResult EditAccidentRecord(int id)
        {
            if (!IsAuthenticated()) return RedirectToLogin(@"~/Admin/EditAccidentRecord/" + id);
            if (!IsAdmin()) return RedirectToAccessDenied();

            ViewBag.ListState = new SelectList(DatabaseController.GetStates(), "ID", "Name");
            ViewBag.ListLGAs = new SelectList(DatabaseController.GetLGAs(), "ID", "Name");
            AccidentRecord data = DatabaseController.GetAccidentRecordByID(id);

            return View(data);
        }

        //
        // POST: /Admin/EditAccidentRecord/
        [HttpPost]
        public ActionResult EditAccidentRecord(AccidentRecord model)
        {
            if (true)
            {
                //string urls = HandleFileUploadsToJson(GetUploadDirectory());
                //model.AccidentPictureURL = urls;
                DatabaseController.EditAccidentRecord(model);
                return RedirectToAction("Success");
            }
            else
            {
                return View();
            }
        }

        // GET: /Admin/CreateConsultant/
        public ActionResult CreateConsultant()
        {
            if (!IsAuthenticated()) return RedirectToLogin(@"~/Admin/CreateConsultant/");
            if (!IsAdmin()) return RedirectToAccessDenied();

            ViewBag.ListState = new SelectList(DatabaseController.GetStates(), "ID", "Name");
            ViewBag.ListLGAs = new SelectList(DatabaseController.GetLGAs(), "ID", "Name");

            return View();
        }

        // POST: /Admin/CreateConsultant/
        [HttpPost]
        public ActionResult CreateConsultant(FormConsultantModel model)
        {
            if (!IsAuthenticated()) return RedirectToLogin(@"~/Admin/CreateConsultant/");
            if (!IsAdmin()) return RedirectToAccessDenied();

            model.AdminID = GetUserInformation().ID;

            DatabaseController.CreateConsultantCommonUser(model);

            return View("Success");
        }

        //
        // GET: /Admin/CreateEvent
        public ActionResult CreateEvent()
        {
            if (!IsAuthenticated()) return RedirectToLogin(@"~/Admin/CreateEvent/");
            if (!IsAdmin()) return RedirectToAccessDenied();

            ViewBag.ListState = new SelectList(DatabaseController.GetStates(), "ID", "Name");
            ViewBag.ListLGAs = new SelectList(DatabaseController.GetLGAs(), "ID", "Name");
            ViewBag.ListPartner = new SelectList(DatabaseController.GetPartners(), "ID", "Name");

            return View();
        }

        //
        // POST: /Admin/CreateEvent
        [HttpPost]
        public ActionResult CreateEvent(Eventt model)
        {
            if (!IsAuthenticated()) return RedirectToLogin(@"~/Admin/CreateEvent/");
            if (!IsAdmin()) return RedirectToAccessDenied();

            model.AdminID = GetUserInformation().ID;
            DatabaseController.CreateEvent(model);

            return RedirectToAction("Success");
        }

        // 
        // GET: /Admin/EditEvent/{id}
        public ActionResult EditEvent(int id)
        {
            if (!IsAuthenticated()) return RedirectToLogin(@"~/Admin/EditEvent/" + id);
            if (!IsAdmin()) return RedirectToAccessDenied();

            ViewBag.ListState = new SelectList(DatabaseController.GetStates(), "ID", "Name");
            ViewBag.ListLGAs = new SelectList(DatabaseController.GetLGAs(), "ID", "Name");
            ViewBag.ListPartner = new SelectList(DatabaseController.GetPartners(), "ID", "Name");

            Eventt data = DatabaseController.GetEventByID(id);

            return View(data);
        }

        // POST: /Admin/EditEvent/
        [HttpPost]
        public ActionResult EditEvent(Eventt model)
        {
            if (!IsAuthenticated()) return RedirectToLogin(@"~/Admin/EditEvent/" + model.ID);
            if (!IsAdmin()) return RedirectToAccessDenied();

            ViewBag.ListState = new SelectList(DatabaseController.GetStates(), "ID", "Name");

            DatabaseController.EditEvent(model);

            return RedirectToAction("Success");
        }

        //
        // GET: /Admin/ListUser
        public ActionResult ListUser(int? page)
        {
            int currentPageIndex = page.HasValue ? page.Value : 1;

            if (!IsAuthenticated()) return RedirectToLogin(@"~/Admin/ListUser/");
            if (!IsAdmin()) return RedirectToAccessDenied();

            var data = GetUserData(currentPageIndex, NumberOfRecordPerPage);

            if (Request.IsAjaxRequest())
                return PartialView("_AjaxUserList", data);
            else
                return View(data);
        }

        private MvcPaging.IPagedList<CommonUser> GetUserData(int currentPageIndex, int numberPerPage)
        {
            var listData = DatabaseController.GetAllCommonUsers();
            var subData = MvcPaging.IQueryableExtensions.ToPagedList(listData, currentPageIndex, numberPerPage);

            return subData;
        }

        // GET: /Admin/CreatePartner/
        public ActionResult CreatePartner()
        {
            if (!IsAuthenticated()) return RedirectToLogin(@"~/Admin/CreatePartner/");
            if (!IsAdmin()) return RedirectToAccessDenied();

            return View();
        }

        // POST: /Admin/CreatePartner/
        [HttpPost]
        public ActionResult CreatePartner(FormPartnerModel model)
        {
            if (!IsAuthenticated()) return RedirectToLogin(@"~/Admin/CreatePartner/");
            if (!IsAdmin()) return RedirectToAccessDenied();

            model.AdminID = GetUserInformation().ID;

            DatabaseController.CreatePartnerCommonUser(model);

            return View("Success");
        }

        //
        // GET: /Admin/ListPartner
        public ActionResult ListPartner(int? page)
        {
            int currentPageIndex = page.HasValue ? page.Value : 1;

            if (!IsAuthenticated()) return RedirectToLogin(@"~/Admin/ListPartner/");
            if (!IsAdmin()) return RedirectToAccessDenied();

            string sName = GetStringParam("sName");
            var data = GetPartnerData(currentPageIndex, NumberOfRecordPerPage, sName);

            if (Request.IsAjaxRequest())
                return PartialView("_AjaxPartnerList", data);
            else
                return View(data);
        }

        private MvcPaging.IPagedList<Partner> GetPartnerData(int currentPageIndex, int numberPerPage, string sName)
        {
            var listData = DatabaseController.GetPartners();

            if (!string.IsNullOrWhiteSpace(sName))
            {
                sName = sName.ToLower();
                listData = from data in listData
                           where data.Name.ToLower().Contains(sName)
                           select data;
            }

            var subData = MvcPaging.IQueryableExtensions.ToPagedList(listData, currentPageIndex, numberPerPage);

            return subData;
        }

        //
        // GET: /Admin/ViewPartner
        public ActionResult ViewPartner(int? id)
        {
            if (!id.HasValue) return RedirectToAction("Index");
            if (!IsAuthenticated()) return RedirectToLogin(@"~/Admin/ViewPartner/" + id);
            if (!IsAdmin()) return RedirectToAccessDenied();

            var data = DatabaseController.GetPartnerByID(id.Value);

            return View(data);
        }

        // GET: /Admin/CreateHumanResource/
        public ActionResult CreateHumanResource()
        {
            if (!IsAuthenticated()) return RedirectToLogin(@"~/Admin/CreateHumanResource/");
            if (!IsAdmin()) return RedirectToAccessDenied();

            ViewBag.ListState = new SelectList(DatabaseController.GetStates(), "ID", "Name");
            ViewBag.ListLGAs = new SelectList(DatabaseController.GetLGAs(), "ID", "Name");

            return View();
        }

        // POST: /Admin/CreateHumanResource/
        [HttpPost]
        public ActionResult CreateHumanResource(FormHumanResourceModel model)
        {
            if (!IsAuthenticated()) return RedirectToLogin(@"~/Admin/CreateHumanResource/");
            if (!IsAdmin()) return RedirectToAccessDenied();

            model.AdminID = GetUserInformation().ID;

            DatabaseController.CreateHumanResourceCommonUser(model);

            return View("Success");
        }

        //
        // GET: /Admin/ListSubscriber
        public ActionResult ListSubscriber(int? page)
        {
            int currentPageIndex = page.HasValue ? page.Value : 1;

            if (!IsAuthenticated()) return RedirectToLogin(@"~/Admin/ListSubscriber/");
            if (!IsAdmin()) return RedirectToAccessDenied();

            var data = GetSubscriberData(currentPageIndex, NumberOfRecordPerPage);

            if (Request.IsAjaxRequest())
                return PartialView("_AjaxSubscriberList", data);
            else
                return View(data);
        }

        private MvcPaging.IPagedList<Subcriber> GetSubscriberData(int currentPageIndex, int numberPerPage)
        {
            var listData = DatabaseController.GetAllSubscribers();
            var subData = MvcPaging.IQueryableExtensions.ToPagedList(listData, currentPageIndex, numberPerPage);

            return subData;
        }

        //
        // GET: /Admin/ViewSubscriber
        public ActionResult ViewSubscriber(int? id)
        {
            if (!id.HasValue) return RedirectToAction("Index");
            if (!IsAuthenticated()) return RedirectToLogin(@"~/Admin/ViewSubscriber/" + id);
            if (!IsAdmin()) return RedirectToAccessDenied();

            var data = DatabaseController.GetSubscriberById(id.Value);

            return View(data);
        }

        //
        // GET: /Admin/ViewRegisterForm
        public ActionResult ViewRegisterForm(int? id)
        {
            if (!id.HasValue) return RedirectToAction("Index");
            if (!IsAuthenticated()) return RedirectToLogin(@"~/Admin/ViewRegisterForm/" + id);
            if (!IsAdmin()) return RedirectToAccessDenied();

            var data = DatabaseController.GetRegisterFormById(id.Value);

            return View(data);
        }
    }
}
